package com.proj.controller;

import com.proj.bean.User;
import com.proj.service.IShoppingLoginService;
import com.proj.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    IShoppingLoginService loginService;

    @RequestMapping("shopping/dologin")
    @ResponseBody
    public String shoppingLogin(User user, HttpSession session){
        //执行登陆业务
//        System.out.println(user);
        User u=loginService.dologin(user);
//        System.out.println(u);
        if(u==null) {
            return "notexist";
        }
        if(!SYS.checkPass(user.getPassword(),u.getPassword())){
            return "passerror";
        }
        //记录登陆状态
        session.setAttribute(SYS.SHOPPING_LOGIN_USER, u);
        return "success";
    }

    @RequestMapping("shopping/passchecker")
    @ResponseBody
    public String passchecking(User user, HttpSession session){
        System.out.println(user);
        //执行登陆业务
//        System.out.println(user);
        User u=loginService.dologin(user);
//        System.out.println(u);
        if(u==null) {
            return "fail";
        }
        if(!SYS.checkPass(user.getPassword(),u.getPassword())){
            return "fail";
        }
        return "success";
    }

    @RequestMapping("sys/dologin")
    @ResponseBody
    public String sysLogin(User user, HttpSession session){
        //执行登陆业务
//        System.out.println(user);
        User u=loginService.dologin(user);
//        System.out.println(u);
        if(u==null) {
            return "fail";
        }
        if(u.getUserlevel()==1){
            return "fail";
        }
        if(!SYS.checkPass(user.getPassword(),u.getPassword())){
            return "fail";
        }
        //记录登陆状态
        session.setAttribute(SYS.SYSTEM_LOGIN_ADMIN, u);
        return "success";
    }
}
